package com.etoak.common.jwt;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;
//创建token 解析token
public class MyJwtUtil {
    public static final String KEY_STRING = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";

    public  static  final Key SING_KEY = Keys.hmacShaKeyFor(KEY_STRING.getBytes());

    public static final long EXPIRE = 1000*60*60*12;
    //创建token
    public static String create(Map<String, Object> claimsMap){
        Date now = new Date();
        return Jwts.builder()
                .signWith(SING_KEY) //签名密钥
                .setClaims(claimsMap) //payload 声明
                .setIssuedAt(now) //签发时间
                .setExpiration(new Date(now.getTime()+EXPIRE)) //过期时间
                .compact();
    }
    //解析token
    public  static  Map<String,Object> parse(String token){
        return Jwts.parserBuilder()
                .setSigningKey(SING_KEY)
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}
